<!DOCTYPE html><html><head><title>README</title><link href="http://www.jboss.org/jdf/stylesheets/documentation.css" rel="stylesheet"></link><link href="http://www.jboss.org/jdf/stylesheets/pygments.css" rel="stylesheet"></link></head><body>
<h1><a id="ejbremote-remote-ejb-client-example" class="anchor" href="#ejbremote-remote-ejb-client-example"><span class="anchor-icon"></span></a>ejb-remote: Remote EJB Client Example</h1>

<p>Author: Jaikiran Pai, Mike Musgrove<br/>
Level: Intermediate<br/>
Technologies: EJB<br/>
Summary: Shows how to access an EJB from a remote Java client program using JNDI<br/>
Target Product: EAP<br/>
Product Versions: EAP 6.1, EAP 6.2<br/>
Source: <a href="https://github.com/jboss-developer/jboss-eap-quickstarts/">https://github.com/jboss-developer/jboss-eap-quickstarts/</a>  </p>

<h2><a id="what-is-it" class="anchor" href="#what-is-it"><span class="anchor-icon"></span></a>What is it?</h2>

<p>This example shows how to access an EJB from a remote Java client application. It demonstrates the use of <em>EJB 3.1</em> and <em>JNDI</em> in Red Hat JBoss Enterprise Application Platform.</p>

<p>There are two components to this example: </p>

<ol>
<li><p>A server side component:</p>

<p>The server component is comprised of a stateful EJB and a stateless EJB. It provides both an EJB JAR that is deployed to the server and a JAR file containing the remote business interfaces required by the remote client application.</p></li>
<li><p>A remote client application that accesses the server component. </p>

<p>The remote client application depends on the remote business interfaces from the server component. This application looks up the stateless and stateful beans via JNDI and invokes a number of methods on them.</p></li>
</ol>

<p>Each component is defined in its own standalone Maven module. The quickstart provides a top level Maven module to simplify the packaging of the artifacts.</p>

<h2><a id="system-requirements" class="anchor" href="#system-requirements"><span class="anchor-icon"></span></a>System requirements</h2>

<p>The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 6.1 or later. </p>

<p>All you need to build this project is Java 6.0 (Java SDK 1.6) or later, Maven 3.0 or later.</p>

<h2><a id="configure-maven" class="anchor" href="#configure-maven"><span class="anchor-icon"></span></a>Configure Maven</h2>

<p>If you have not yet done so, you must <a href="../README.html#configure-maven">Configure Maven</a> before testing the quickstarts.</p>

<h2><a id="start-the-jboss-server" class="anchor" href="#start-the-jboss-server"><span class="anchor-icon"></span></a>Start the JBoss Server</h2>

<ol>
<li>Open a command line and navigate to the root of the JBoss server directory.</li>
<li><p>The following shows the command line to start the server:</p>
<div class="highlight"><pre><span class="n">For</span> <span class="n">Linux</span><span class="o">:</span>   <span class="n">JBOSS_HOME</span><span class="o">/</span><span class="n">bin</span><span class="o">/</span><span class="n">standalone</span><span class="p">.</span><span class="n">sh</span>
<span class="n">For</span> <span class="n">Windows</span><span class="o">:</span> <span class="n">JBOSS_HOME</span><span class="err">\</span><span class="n">bin</span><span class="err">\</span><span class="n">standalone</span><span class="p">.</span><span class="n">bat</span>
</pre></div></li>
</ol>

<h2><a id="build-and-deploy-the-quickstart" class="anchor" href="#build-and-deploy-the-quickstart"><span class="anchor-icon"></span></a>Build and Deploy the Quickstart</h2>

<p>Since this quickstart builds two separate components, you can not use the standard <em>Build and Deploy</em> commands used by most of the other quickstarts. You must follow these steps to build, deploy, and run this quickstart.</p>

<ol>
<li>Make sure you have started the JBoss server. See the instructions in the previous section.</li>
<li>Open a command line and navigate to the ejb-remote quickstart directory</li>
<li><p>Build and install the server side component:</p>

<ul>
<li><p>Navigate to the server-side subdirectory:</p>

<p>cd server-side</p></li>
<li><p>Build the EJB and client interfaces JARs and install them in your local Maven repository.</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">clean</span> <span class="n">install</span>        
</pre></div></li>
<li><p>Deploy the EJB JAR to your server. This maven goal will deploy <code>server-side/target/jboss-ejb-remote-server-side.jar</code>. You can check the JBoss server console to see information messages regarding the deployment.</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">jboss</span><span class="o">-</span><span class="n">as</span><span class="o">:</span><span class="n">deploy</span>
</pre></div></li>
</ul></li>
<li><p>Build and run the client application</p>

<ul>
<li><p>Navigate to the server-side subdirectory:</p>
<div class="highlight"><pre><span class="n">cd</span> <span class="p">..</span><span class="o">/</span><span class="n">client</span>
</pre></div></li>
<li><p>Compile the client code</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">clean</span> <span class="n">compile</span>
</pre></div></li>
<li><p>Execute the client application within Maven</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">exec</span><span class="o">:</span><span class="n">exec</span>
</pre></div></li>
</ul></li>
</ol>

<h2><a id="investigate-the-console-output" class="anchor" href="#investigate-the-console-output"><span class="anchor-icon"></span></a>Investigate the Console Output</h2>

<p>When the client application runs, it performs the following steps:</p>

<ol>
<li>Obtains a stateless session bean instance.</li>
<li>Sends method invocations to the stateless bean to add two numbers, and then displays the result.</li>
<li>Sends a second invocation to the stateless bean subtract two numbers, and then displays the result.</li>
<li>Obtains a stateful session bean instance.</li>
<li>Sends several method invocations to the stateful bean to increment a field in the bean, displaying the result each time.</li>
<li>Sends several method invocations to the stateful bean to decrement a field in the bean, displaying the result each time.</li>
</ol>

<p>The output in the terminal window  will look like the following:</p>
<div class="highlight"><pre>  <span class="n">Obtained</span> <span class="n">a</span> <span class="n">remote</span> <span class="n">stateless</span> <span class="n">calculator</span> <span class="k">for</span> <span class="n">invocation</span>
  <span class="n">Adding</span> <span class="mi">204</span> <span class="n">and</span> <span class="mi">340</span> <span class="n">via</span> <span class="n">the</span> <span class="n">remote</span> <span class="n">stateless</span> <span class="n">calculator</span> <span class="n">deployed</span> <span class="n">on</span> <span class="n">the</span> <span class="n">server</span>
  <span class="n">Remote</span> <span class="n">calculator</span> <span class="n">returned</span> <span class="n">sum</span> <span class="o">=</span> <span class="mi">544</span>
  <span class="n">Subtracting</span> <span class="mi">2332</span> <span class="n">from</span> <span class="mi">3434</span> <span class="n">via</span> <span class="n">the</span> <span class="n">remote</span> <span class="n">stateless</span> <span class="n">calculator</span> <span class="n">deployed</span> <span class="n">on</span> <span class="n">the</span> <span class="n">server</span>
  <span class="n">Remote</span> <span class="n">calculator</span> <span class="n">returned</span> <span class="n">difference</span> <span class="o">=</span> <span class="mi">1102</span>
  <span class="n">Obtained</span> <span class="n">a</span> <span class="n">remote</span> <span class="n">stateful</span> <span class="n">counter</span> <span class="k">for</span> <span class="n">invocation</span>
  <span class="n">Counter</span> <span class="n">will</span> <span class="n">now</span> <span class="n">be</span> <span class="n">incremented</span> <span class="mi">5</span> <span class="n">times</span>
  <span class="n">Incrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">increment</span> <span class="n">is</span> <span class="mi">1</span>
  <span class="n">Incrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">increment</span> <span class="n">is</span> <span class="mi">2</span>
  <span class="n">Incrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">increment</span> <span class="n">is</span> <span class="mi">3</span>
  <span class="n">Incrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">increment</span> <span class="n">is</span> <span class="mi">4</span>
  <span class="n">Incrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">increment</span> <span class="n">is</span> <span class="mi">5</span>
  <span class="n">Counter</span> <span class="n">will</span> <span class="n">now</span> <span class="n">be</span> <span class="n">decremented</span> <span class="mi">5</span> <span class="n">times</span>
  <span class="n">Decrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">decrement</span> <span class="n">is</span> <span class="mi">4</span>
  <span class="n">Decrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">decrement</span> <span class="n">is</span> <span class="mi">3</span>
  <span class="n">Decrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">decrement</span> <span class="n">is</span> <span class="mi">2</span>
  <span class="n">Decrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">decrement</span> <span class="n">is</span> <span class="mi">1</span>
  <span class="n">Decrementing</span> <span class="n">counter</span>
  <span class="n">Count</span> <span class="n">after</span> <span class="n">decrement</span> <span class="n">is</span> <span class="mi">0</span>
</pre></div>
<p>Logging statements have been removed from this output here to make it clearer.</p>

<h2><a id="build-and-run-the-quickstart-as-an-executable-jar" class="anchor" href="#build-and-run-the-quickstart-as-an-executable-jar"><span class="anchor-icon"></span></a>Build and Run The Quickstart as an Executable JAR</h2>

<p>The remote client application can also be built as a standalone executable JAR with all of its dependencies.</p>

<ol>
<li><p>Open a command line and navigate to the ejb-remote/client quickstart directory</p>
<div class="highlight"><pre><span class="n">cd</span> <span class="n">client</span>
</pre></div></li>
<li><p>Type the following in the command line:</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">clean</span> <span class="n">assembly</span><span class="o">:</span><span class="n">assembly</span>
</pre></div></li>
<li><p>You can then run the executable JAR using the <code>java -jar</code> command. You will see the same console output as above.</p>
<div class="highlight"><pre><span class="n">java</span> <span class="o">-</span><span class="n">jar</span> <span class="n">target</span><span class="o">/</span><span class="n">jboss</span><span class="o">-</span><span class="n">ejb</span><span class="o">-</span><span class="n">remote</span><span class="o">-</span><span class="n">client</span><span class="o">-</span><span class="n">jar</span><span class="o">-</span><span class="n">with</span><span class="o">-</span><span class="n">dependencies</span><span class="p">.</span><span class="n">jar</span> 
</pre></div></li>
</ol>

<h2><a id="undeploy-the-archive" class="anchor" href="#undeploy-the-archive"><span class="anchor-icon"></span></a>Undeploy the Archive</h2>

<p>To undeploy the server side component from the JBoss server:</p>

<ol>
<li><p>Navigate to the server-side subdirectory:</p>
<div class="highlight"><pre><span class="n">cd</span> <span class="p">..</span><span class="o">/</span><span class="n">server</span><span class="o">-</span><span class="n">side</span>
</pre></div></li>
<li><p>Type the following command:</p>
<div class="highlight"><pre><span class="n">mvn</span> <span class="n">jboss</span><span class="o">-</span><span class="n">as</span><span class="o">:</span><span class="n">undeploy</span>
</pre></div></li>
</ol>

<h2><a id="run-the-quickstart-in-jboss-developer-studio-or-eclipse" class="anchor" href="#run-the-quickstart-in-jboss-developer-studio-or-eclipse"><span class="anchor-icon"></span></a>Run the Quickstart in JBoss Developer Studio or Eclipse</h2>

<p>You can also start the server and deploy the quickstarts from Eclipse using JBoss tools. For more information, see <a href="../README.html#use-jboss-developer-studio-or-eclipse-to-run-the-quickstarts">Use JBoss Developer Studio or Eclipse to Run the Quickstarts</a> </p>

<h2><a id="debug-the-application" class="anchor" href="#debug-the-application"><span class="anchor-icon"></span></a>Debug the Application</h2>

<p>If you want to debug the source code or look at the Javadocs of any library in the project, run either of the following commands to pull them into your local repository. The IDE should then detect them.</p>
<div class="highlight"><pre>    <span class="n">mvn</span> <span class="n">dependency</span><span class="o">:</span><span class="n">sources</span>
    <span class="n">mvn</span> <span class="n">dependency</span><span class="o">:</span><span class="n">resolve</span> <span class="o">-</span><span class="n">Dclassifier</span><span class="o">=</span><span class="n">javadoc</span>
</pre></div></body></html>
